package com.terabits.smartriver.netty.config;

import com.terabits.smartriver.util.PacketUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 解码
 */
@Component
public class PacketDecoder extends MessageToMessageDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        if (msg == null) {
            return;
        }
        System.out.println("PacketDecoder:" + msg);
        byte[] buffer = new byte[msg.readableBytes()];
        msg.getBytes(0, buffer);
        short[] buff = PacketUtil.verify(buffer);
        System.out.println("PacketDecoderbuff:" + buff);
        out.add(PacketUtil.parsePacket2Bean(buff));

    }
}
